import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.*;
public class Calculator {

    Transform t = new Transform();
    Mystack q = new Mystack();
    Rational result = new Rational();
    Calculator(){}
    Calculator(StringBuffer s){
        q = new Mystack(t.tran(s));
    }

    public void jisuan() {
        int i = 0;
        Rational a,b;
        while (q.size()!=1) {
            while (i + 2 <= q.size()) {
                if ((q.isNumeric(i)||q.isRational(i))&&(q.isNumeric(i+1)||q.isRational(i+1))) {
                    if (q.peek(i + 2).equals("+") ||q.peek(i + 2).equals("-")  || q.peek(i + 2).equals("*")  || q.peek(i + 2).equals("/") ) {
                        if (q.isNumeric(i))
                            a = new Rational(Integer.parseInt(q.peek(i)));
                        else
                            a = new Rational(q.peek(i));
                        if (q.isNumeric(i+1))
                            b = new Rational(Integer.parseInt(q.peek(i + 1)));
                        else
                            b = new Rational(q.peek(i+1));

                        switch (q.peek(i + 2)) {
                            case "+":
                                result = a.add(b);
                                break;
                            case "-":
                                result = a.sub(b);
                                break;
                            case "*":
                                result = a.muti(b);
                                break;
                            case "/":
                                result = a.div(b);
                                break;


                        }
                        q.put(result.toString(),i);
                        q.move(i+1);
                        i=0;
                    }
                    else
                        i++;
                }
                else
                    i++;
            }
        }
        return;
    }
}

